<%--
  -	Date: 2012-05-22
  -
  - Copyright (C) 2012 Jawa Software. All rights reserved.
  - 
  - 用户管理页面。
--%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@ page import="org.jawa.util.ParamUtils,
	        	 org.jawa.util.JawaGlobals,
	        	 org.jawa.JawaServer,
	        	 org.jawa.user.User,
	        	 org.jawa.user.UserManager"
	        	 
%>
<%@ page import="java.util.Collection,
                 java.util.List,
                 java.util.ArrayList"
%>
<%@ page import="org.apache.commons.lang.StringUtils"%>

<jsp:useBean id="webManager" class="org.jawa.util.WebManager"  />
<% 
    webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>

<%
	int range = ParamUtils.getIntParameter(request, "range", 20);
	int start = webManager.getPageIndex(range,"user");
	
	Collection<User> users = webManager.getUserManager().getUsers(start,range);
	int userCount = webManager.getUserManager().getUserCount();
	
	
	String br_id = ParamUtils.getParameter(request,"brId");
	String login_no = ParamUtils.getParameter(request,"loginNo");
	String user_name = ParamUtils.getParameter(request,"userName");
	
	br_id = StringUtils.trimToEmpty(br_id);
	login_no = StringUtils.trimToEmpty(login_no);
	user_name = StringUtils.trimToEmpty(user_name);
	

	
	if (request.getParameter("search") != null && !request.getParameter("search").equals(""))
	{
		String[] formFields = new String[]{"br_id","user_name"};
		
		List<String> fields = new ArrayList<String>();
		List<String>  querys = new ArrayList<String>();
		
		if(StringUtils.isNotEmpty(br_id)){
			fields.add("br_id");
			querys.add(br_id);
		}
		if(StringUtils.isNotEmpty(login_no)){
			fields.add("login_no");
			querys.add(login_no);
		}
		if(StringUtils.isNotEmpty(user_name)){
			fields.add("user_name");
			querys.add("%"+user_name+"%");
		}
		
		users = webManager.getUserManager().findUsers(fields,querys,start,range);
	    userCount = webManager.getUserManager().findUsers(fields,querys).size();
	}
	
	
	request.setAttribute("users",users);
	request.setAttribute("userCount",userCount);
	request.setAttribute("range",range);
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>用户管理</title>
		<meta name="pageID" content="1202" />
	</head>
	<body>
		<div class="row-fluid">

			<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">系统管理</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					用户管理
				</li>
			</ul>

			<div class="page-header hide">
				<h3>
					用户管理
				</h3>
			</div>

			<form action="index.jsp" method="post" class="form-search well">
				<div class="row-fluid ">
						<label class="query-label">
							用户机构:
						</label>
						<input type="text" name="brId" value="<%=br_id %>" class="input-small " >
						<label class="query-label">
							用户编号:
						</label>
						<input type="text" name="loginNo" value="<%=login_no %>" class="input-small " >
						<label class="query-label">
							用户姓名:
						</label>
						<input type="text" name="userName" value="<%=user_name %>" class="input-small " >
						<input type="submit" name="search" class="btn btn-primary" value="查询">
				</div>
			</form>
			
			<div class="subnav">
			    <div class="btn-toolbar pull-left">
			        <%
			        	if("true".equals(request.getParameter("create")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						    创建新用户成功!
						</p>
					<%  
						} 
					%>
			        <%
			        	if("true".equals(request.getParameter("update")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						   修改用户成功!
						</p>
					<%  
						} 
			        %>
			        <%
		        	    if("true".equals(request.getParameter("delete")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						   删除用户成功!
						</p>
					<%  
						} 
					%>
			    </div>
			    <div class="btn-toolbar pull-right">
					<a class="btn btn-success " href="user-form.jsp"><i class="icon-user icon-white"></i>增加用户</a>
			    </div>
			</div>

				<display:table id="user" name="users" partialList="true" 
					size="${userCount}" pagesize="${range}" requestURI="index.jsp" export="false"
					class="table table-striped table-bordered table-condensed">
					<display:column title="用户部门">
						<c:out value="${user.branch.brName}" />
					</display:column>
					<display:column title="用户编号">
						<c:out value="${user.loginNo}" />
					</display:column>
					<display:column title="用户姓名">
						<c:out value="${user.userName}" />
					</display:column>
					<display:column title="性别">
						<c:if test="${user.userSex=='0'}">男</c:if>
						<c:if test="${user.userSex=='1'}">女</c:if>
					</display:column>
					<display:column title="类型">
						<c:if test="${user.userType=='0'}">一般用户</c:if>
						<c:if test="${user.userType=='1'}">管理用户</c:if>
					</display:column>
					<display:column title="状态">
						<c:if test="${user.userStatus=='0'}">正常</c:if>
						<c:if test="${user.userStatus=='1'}">禁用</c:if>
					</display:column>
					<display:column title="操作" style="width:15%;">
					    <div class="btn-group">
							<a href="#" onclick="return deleteUser('${user.userId }');" class="btn btn-mini"><i class="icon-trash "></i>删除</a>
							<a href="user-form.jsp?edit=true&userId=${user.userId}" class="btn btn-mini"><i class="icon-edit "></i>修改</a>
					    </div>
					</display:column>
				</display:table>

		</div>

		<script src="<%= path %>/scripts/bootstrap.js"></script>
		<script>
			function deleteUser(userId){
				if(confirm('你确定要删除当前用户吗?')){
					window.location.href='user-form.jsp?delete=true&userId='+userId;
				}
			}
		</script>
		
	</body>